clear
set more off
 
* --------------------------------------------------------
* Table 1. Unido data

local name="table1"

use ./data/unido1, clear

ta country if va != . & wagebill != .
local j = 1
local countries "Argentina Brazil Chile Colombia Ecuador Mexico Peru"
foreach c of local countries {
  count if country== "`c'" & va != . & wagebill != .
  local tot_obs_`j' = r(N)

  unique year if country == "`c'" & va != . & wagebill != .
  local years_`j' = r(unique)

  local j = `j' + 1
}
* All countries
count if va != . & wagebill != .
local tot_obs_8 = r(N)
local years_8 = `years_1' + `years_2' + `years_3' + `years_4' + `years_5' + `years_6' + `years_7'

* columns
use ./data/unido1, clear

gen periodo=.
replace periodo=1 if year>=1992 & year<2005
replace periodo=2 if year>=2005 & year<=2015
drop if periodo==.
*foreach x of varlist emp va wagebill out {
foreach x of varlist va wagebill {
  bys country periodo: egen aux_`x'=sum(`x')
  drop `x'
  rename aux_`x' `x'
}
bys country periodo: drop if _n>1
capture drop share

* variables
gen share=wagebill/va
*gen w=wagebill/emp
*gen val=va/emp

* table
qui {
set linesize 255
capture log close
log using "./results/`name'.tex", text replace
no di "\begin{tabular}{lccccccc}"
no di "\hline\hline"

    * 1 (pa�s) + 2 (obs) + 1 (espaciador) + 4 (labor share) = 8 columnas
no di " & \multicolumn{2}{c}{\rule{0cm}{0.5cm}Observations}  &  & \multicolumn{4}{c}{\rule{0cm}{0.5cm}Labor share} \\"
no di "\cline{2-3} \cline{5-8}"

no di " & \rule{0cm}{0.5cm} Total & Years &  & All years & 1992--2004 & 2005--2015 & Change \\"
no di " & (1) & (2) &  & (3) & (4) & (5) & (6)  \\"
no di "\hline"

local j = 1
foreach i in "Argentina" "Brazil" "Chile" "Colombia" "Ecuador" "Mexico" "Peru" {
 sum share if country=="`i'"
 local e1=r(mean)
 sum share if country=="`i'" & periodo==1
 local a1=0
 local a1=r(mean)
 sum share if country=="`i'" & periodo==2
 local b1=0
 local b1=r(mean)
 local d1 = (`b1'-`a1')
 if "`i'"=="Argentina" {
   no di "\rule{0cm}{0.5cm}`i' & " %9.0f `tot_obs_1' " & " %9.0f `years_1' " & & " %9.2f `e1' "&"  %9.2f `a1' "&" %9.2f `b1' "&" %9.3f `d1' "\\"
 }
 if "`i'"~="Argentina" {
   no di "`i' & " %9.0f `tot_obs_`j'' " & " %9.0f `years_`j'' " & & " %9.2f `e1' "&" %9.2f `a1' "&" %9.2f `b1' "&" %9.3f `d1' "\\"
 }
local j = `j' + 1 

}

no di "\hline"
local i="All countries"
sum share if country~="Argentina"
local e1=r(mean)
sum share if periodo==1 & country~="Argentina"
local a1=0
local a1=r(mean)
sum share if periodo==2 & country~="Argentina"
local b1=0
local b1=r(mean)
local d1 = (`b1'-`a1')
no di "\textbf{\rule{0cm}{0.5cm}`i'} & " %9.0f `tot_obs_8' " & " %9.0f `years_8' " & & " %9.2f `e1' "&"  %9.2f `a1' "&" %9.2f `b1' "&" %9.3f `d1' "\\"

no di "\hline"
no di "\end{tabular}"

log close
}
 
* --------------------------------------------------------
* Table 2. SEDLAC data

use ./data/sedlac1, clear
gen order = .
replace order = 1 if country == "Argentina"
replace order = 2 if country == "Brazil"
replace order = 3 if country == "Chile"
replace order = 4 if country == "Colombia"
replace order = 5 if country == "Ecuador"
replace order = 6 if country == "Mexico"
replace order = 7 if country == "Peru"
replace order = 8 if country == "All countries"
sort order

gen str12 households_s    = string(households,    "%12.0f")
gen str8  districtyear_s  = string(districtyear,  "%8.0f")
gen str8 years_s = ""
replace years_s = string(years, "%8.1f") if country == "All countries"
replace years_s = string(years, "%8.0f") if country != "All countries"
gen str8 unemprate_s    = string(unemprate,    "%8.1f")
gen str8 laborincome_s  = string(laborincome,  "%8.1f")
gen str8 povertyrate_s  = string(povertyrate,  "%8.1f")
gen str8 ratio_s        = string(ratiop75p25,  "%8.2f")

local outfile "./results/table2.tex"
cap file close myfile
file open myfile using "`outfile'", write replace text
file write myfile "\begin{tabular}{lccccccc}" _n
file write myfile "\hline\hline" _n
file write myfile " & Households & District- & Years & Unemp. & Labor & Poverty & Ratio \\" _n
file write myfile " &  & Year &  & rate & income & rate & p75/p25 \\" _n
file write myfile "\cline{2-2}\cline{3-3}\cline{4-4}\cline{5-5}\cline{6-6}\cline{7-7}\cline{8-8}" _n
file write myfile " & (1) & (2) & (3) & (4) & (5) & (6) & (7) \\" _n
file write myfile "\hline" _n
quietly {
    forvalues i = 1/`=_N' {
        local c  = country[`i']
        local h  = households_s[`i']
        local dy = districtyear_s[`i']
        local y  = years_s[`i']
        local u  = unemprate_s[`i']
        local li = laborincome_s[`i']
        local p  = povertyrate_s[`i']
        local r  = ratio_s[`i']
        if "`c'" == "All countries" {
            file write myfile "\hline" _n
        }
        file write myfile "`c' & `h' & `dy' & `y' & `u' & `li' & `p' & `r' \\" _n
    }
}
file write myfile "\hline\hline" _n
file write myfile "\end{tabular}" _n
file close myfile
 
* --------------------------------------------------------
* Table 3. Industries and occupations with high and low levels of routinization

local name="table3"

* industries
use ./data/piaac3, clear
bys isic: egen aux=mean(RTJ8)
bys isic: keep if _n==1
drop if isic==16
gsort -RTC1lac
keep if _n<=3 | _n>=17

qui { 
set linesize 255
capture log close
log using "./results/`name'.tex", text replace
no di "\begin{tabular}"
no di "{lcccccc}\hline\hline"
no di " & \rule{0cm}{0.5cm}RTC1 & RTC2 & RTC3 & RTC4 & RTC5 & RTC6 \\"
no di "& (1) & (2) & (3) & (4) & (5) & (6) \\ \hline"
no di " \rule{0cm}{0.5cm}\textbf{Industries} &  &  & &  &  & \\"
local i=1
no di "\rule{0cm}{0.5cm}" isicname[`i'] "&" %9.2f RTC1lac[`i'] "&" %9.2f RTC2lac[`i'] "&" %9.2f RTC3lac[`i'] "&" %9.2f RTC4lac[`i'] "&" %9.2f RTC5lac[`i'] "&" %9.2f aux[`i'] "\\"
forvalues i=2(1)3 {
  no di isicname[`i'] "&" %9.2f RTC1lac[`i'] "&" %9.2f RTC2lac[`i'] "&" %9.2f RTC3lac[`i'] "&" %9.2f RTC4lac[`i'] "&" %9.2f RTC5lac[`i'] "&" %9.2f aux[`i'] "\\"
}
no di "\cline{2-7}"
local i=4
no di "\rule{0cm}{0.5cm}" isicname[`i'] "&" %9.2f RTC1lac[`i'] "&" %9.2f RTC2lac[`i'] "&" %9.2f RTC3lac[`i'] "&" %9.2f RTC4lac[`i'] "&" %9.2f RTC5lac[`i'] "&" %9.2f aux[`i'] "\\"
forvalues i=5(1)6 {
  no di isicname[`i'] "&" %9.2f RTC1lac[`i'] "&" %9.2f RTC2lac[`i'] "&" %9.2f RTC3lac[`i'] "&" %9.2f RTC4lac[`i'] "&" %9.2f RTC5lac[`i'] "&" %9.2f aux[`i'] "\\"
}
no di "\hline"
log off
}

* occupations
use ./data/piaac2, clear
gsort -RTC1lac
keep if _n<=3 | _n>=38

qui { 
log on
no di " \rule{0cm}{0.5cm}\textbf{Occupations} &  &  & &  &  & \\"
local i=1
no di "\rule{0cm}{0.5cm}" occname[`i'] "&" %9.2f RTC1lac[`i'] "&" %9.2f RTC2lac[`i'] "&" %9.2f RTC3lac[`i'] "&" %9.2f RTC4lac[`i'] "&" %9.2f RTC5lac[`i'] "&" %9.2f RTC8[`i']  "\\"
forvalues i=2(1)3 {
  no di occname[`i'] "&" %9.2f RTC1lac[`i'] "&" %9.2f RTC2lac[`i'] "&" %9.2f RTC3lac[`i'] "&" %9.2f RTC4lac[`i'] "&" %9.2f RTC5lac[`i'] "&" %9.2f RTC8[`i'] "\\"
}
no di "\cline{2-7}"
local i=4
no di "\rule{0cm}{0.5cm}" occname[`i'] "&" %9.2f RTC1lac[`i'] "&" %9.2f RTC2lac[`i'] "&" %9.2f RTC3lac[`i'] "&" %9.2f RTC4lac[`i'] "&" %9.2f RTC5lac[`i'] "&" %9.2f RTC8[`i']  "\\"
forvalues i=5(1)6 {
  no di occname[`i'] "&" %9.2f RTC1lac[`i'] "&" %9.2f RTC2lac[`i'] "&" %9.2f RTC3lac[`i'] "&" %9.2f RTC4lac[`i'] "&" %9.2f RTC5lac[`i'] "&" %9.2f RTC8[`i']  "\\"
}
no di "\hline"
no di "\end{tabular}"
log close
}

* --------------------------------------------------------
* Figure 1 and Figure A2. Histograms

use ./data/datallm1, clear

bys country district: keep if _n==1
keep country district RTC1

local r="RTC1"

local name="figure1"
histogram `r' if RTC1>0.3, bin(20) frequency fcolor(emidblue) lcolor(white) xtitle(RTC index, size(vlarge)) ytitle(, size(vlarge)) graphregion(fcolor(white))
  graph export ./results/`name'.png, width(1200) replace

local name="figureA2"
histogram `r' if RTC1>0.3 & country=="ARG", bin(20) frequency fcolor(emidblue) lcolor(white) ytitle(, size(vlarge)) xtitle(RTC index) xtitle(, size(vlarge)) title(Argentina, size(vhuge)) graphregion(fcolor(white))
  graph export ./results/`name'-3.png, width(1200) replace
histogram `r' if RTC1>0.3 & country=="BRA", bin(20) frequency fcolor(emidblue) lcolor(white) ytitle(, size(vlarge)) xtitle(RTC index) xtitle(, size(vlarge)) title(Brazil, size(vhuge)) graphregion(fcolor(white))
  graph export ./results/`name'-4.png, width(1200) replace
histogram `r' if RTC1>0.3 & country=="CHL", bin(20) frequency fcolor(emidblue) lcolor(white) ytitle(, size(vlarge)) xtitle(RTC index) xtitle(, size(vlarge)) title(Chile, size(vhuge)) graphregion(fcolor(white))
  graph export ./results/`name'-5.png, width(1200) replace
histogram `r' if RTC1>0.3 & country=="COL", bin(20) frequency fcolor(emidblue) lcolor(white) ytitle(, size(vlarge)) xtitle(RTC index) xtitle(, size(vlarge)) title(Colombia, size(vhuge)) graphregion(fcolor(white))
  graph export ./results/`name'-6.png, width(1200) replace
histogram `r' if RTC1>0.3 & country=="ECU", bin(20) frequency fcolor(emidblue) lcolor(white) ytitle(, size(vlarge)) xtitle(RTC index) xtitle(, size(vlarge)) title(Ecuador, size(vhuge)) graphregion(fcolor(white))
  graph export ./results/`name'-7.png, width(1200) replace
histogram `r' if RTC1>0.3 & country=="MEX", bin(20) frequency fcolor(emidblue) lcolor(white) ytitle(, size(vlarge)) xtitle(RTC index) xtitle(, size(vlarge)) title(Mexico, size(vhuge)) graphregion(fcolor(white))
  graph export ./results/`name'-8.png, width(1200) replace
histogram `r' if RTC1>0.3 & country=="PER", bin(20) frequency fcolor(emidblue) lcolor(white) ytitle(, size(vlarge)) xtitle(RTC index) xtitle(, size(vlarge)) title(Peru, size(vhuge)) graphregion(fcolor(white))
  graph export ./results/`name'-9.png, width(1200) replace

* --------------------------------------------------------
* Figure 2. Trends in adoption of technology

local name="figure2"

use ./data/robots2, clear
foreach i  in "Argentina" "Brazil" "Chile" "Colombia" "Mexico" "Peru" {
twoway (line robots year if country=="`i'", sort lcolor(navy) lw(thick)), ///
    title("`i'", size(vhuge) bcolor(white)) ///
	xtitle("") xlabel(1995(10)2015, grid format(%9.0f) labsize(vlarge)) xline(2005, lc(gray) lpat(dash) lwidth(thick)) ///
    ytitle("") ylabel(minmax, format(%9.0f) labsize(medium)) ///
    legend(off)	 plotregion(icolor(white)) graphregion(fcolor(white) color(white)) bgcolor(white)
graph export "./results/`name'-`i'.png", width(1200) replace
}

* --------------------------------------------------------
* Table 4. Industry level

local name="table4"

use ./data/dataind2, clear

matrix define A=J(20,6,.)
matrix define B=J(20,6,.)
matrix define N=J(20,6,.)

local i=1
local list1 "RTC1 RTC2 RTC3 RTC4 RTC5 RTC6"
local vars1="share1_t0 prod1_t0 delta_prod1"
foreach r of local list1 {
local j=1
foreach y in "delta_lemp" "delta_lwage" "delta_share1"    {
reg `y' `r' `vars1' if t==1, robust
 matrix A[`i',1+(`j'-1)*2]=_b[`r']
 matrix B[`i',1+(`j'-1)*2]=_se[`r']
 matrix N[`i',1+(`j'-1)*2]=e(N)
reg `y' `r' `vars1' if t==2, robust
 matrix A[`i',1+1+(`j'-1)*2]=_b[`r']
 matrix B[`i',1+1+(`j'-1)*2]=_se[`r']
 matrix N[`i',1+1+(`j'-1)*2]=e(N)
local j=`j'+1
} 
local i=`i'+1
} 

*** table
qui { 
set linesize 255
capture log close
log using "./results/`name'.tex", text replace
no di "\begin{tabular}"
no di "{lcccccccc}\hline\hline"
no di " & \multicolumn{2}{c}{\rule{0cm}{0.5cm}Log employment} && \multicolumn{2}{c}{\rule{0cm}{0.5cm}Log wage} && \multicolumn{2}{c}{\rule{0cm}{0.5cm}Labor share}  \\"
no di "& $ (y_{t2}-y_{t1}) $ & $ (y_{t3}-y_{t2}) $ && $ (y_{t2}-y_{t1}) $ & $ (y_{t3}-y_{t2}) $ && $ (y_{t2}-y_{t1}) $ & $ (y_{t3}-y_{t2}) $ \\"
no di "\cline{2-3} \cline{5-6} \cline{8-9}"
no di "& \rule{0cm}{0.5cm}(1) & (2) && (3) & (4) && (5) & (6)\\ \hline"
local l=1
foreach h of local list1 {
  * country name
  no di "\multicolumn{5}{l}{\textbf{\rule{0cm}{0.5cm}`h'}}\\"
  * coefs
  no di "", _c
  forvalues j=1(1)6 {
	no di "&" %9.2f A[`l',`j'], _c
	if `j'==2 | `j'==4  {
	no di "&", _c
	}
  }
  no di "\\"
  * se
  forvalues j=1(1)6{
	local t=abs(A[`l',`j']/B[`l',`j'])
	local stars=""
	if `t'>=abs(invnormal(0.05)) {
	  local stars="*" 
	}
	if `t'>=abs(invnormal(0.025)) {
	  local stars="**"
	}
	if `t'>=abs(invnormal(0.005)) {
	  local stars="***"
	}
	no di "&" "(" %9.2f B[`l',`j'] "`stars')", _c
	if `j'==2 | `j'==4  {
	no di "&", _c
	}
  }
  no di "\\ \hline"
  local l=`l'+1
}
    * N
  no di "\rule{0cm}{0.5cm}Obs. ", _c
  forvalues j=1(1)6 {
	no di "&" %9.0f N[1,`j'], _c
	if `j'==2 | `j'==4  {
	no di "&", _c
	}
  }
  no di "\\ \hline"
no di "\end{tabular}"
log close
}

* --------------------------------------------------------
* Table 5. Parameterization of technology adoption

local name="table5"

use ./data/dataind3, clear

matrix define A=J(20,6,.)
matrix define B=J(20,6,.)
matrix define N=J(20,6,.)

local list1 "RTC1 RTC2 RTC3 RTC4 RTC5 RTC6"
local v="Ddelta_robot_wrk"
local vars1="share1_t0 prod1_t0 Ddelta_prod1"
local i=1
foreach r of local list1 {
 qui gen inter=`r'*`v'
 local j=1
 foreach y in "Ddelta_lemp" "Ddelta_lwage" "Ddelta_share1" "Ddelta_shareemp1" "Ddelta_shareout" "Ddelta_lout" {
  xi: reg `y' inter `r' `vars1' i.year if year>=2004, robust
  matrix A[`i',`j']=_b[inter]
  matrix B[`i',`j']=_se[inter]
  matrix N[`i',`j']=e(N)
  local j=`j'+1
 }
 qui drop inter
 local i=`i'+1
}

* table
qui { 
set linesize 255
capture log close
log using "./results/`name'.tex", text replace
no di "\begin{tabular}"
no di "{lccc}\hline\hline"
no di " & \rule{0cm}{0.5cm}Log employment & Log wage & Labor share  \\"
no di "& $ (y_{t}-y_{0}) $ & $ (y_{t}-y_{0}) $ & $ (y_{t}-y_{0}) $ \\"
no di " \cline{2-4}"
no di "& \rule{0cm}{0.5cm}(1) & (2) & (3) \\ \hline"
local l=1
foreach h of local list1 {
  no di "\multicolumn{4}{l}{\textbf{\rule{0cm}{0.5cm}`h'}}\\"
  no di "", _c
  no di "$ RTC \times Tech$", _c
  forvalues j=1(1)3 {
  	no di "&" %9.2f A[`l',`j'], _c
  }
  no di "\\"
  forvalues j=1(1)3{
	local t=abs(A[`l',`j']/B[`l',`j'])
	local stars=""
	if `t'>=abs(invnormal(0.05)) {
	  local stars="*" 
	}
	if `t'>=abs(invnormal(0.025)) {
	  local stars="**"
	}
	if `t'>=abs(invnormal(0.005)) {
	  local stars="***"
	}
	no di "&" "(" %9.2f B[`l',`j'] "`stars')", _c
  }
  no di "\\ \hline"
local l=`l'+1
}
no di "\rule{0cm}{0.5cm}Obs. ", _c
forvalues j=1(1)3 {
  no di "&" %9.0f N[1,`j'], _c
}
no di "\\ \hline"
no di "\end{tabular}"
log close
}

* --------------------------------------------------------
* Table 6. Share in employment

local name="table6"

matrix define A=J(20,6,.)
matrix define B=J(20,6,.)
matrix define N=J(20,6,.)
local list1 "RTC1 RTC2 RTC3 RTC4 RTC5 RTC6"

use ./data/dataind3, clear
local i=1
foreach r of local list1 {

xi: reg shareemp1 `r' i.country if year==2005, robust
  matrix A[`i',1]=_b[`r']
  matrix B[`i',1]=_se[`r']
  matrix N[`i',1]=e(N)
xi: reg shareemp1 `r' i.country if year==2013, robust
  matrix A[`i',2]=_b[`r']
  matrix B[`i',2]=_se[`r']
  matrix N[`i',2]=e(N)
local i=`i'+1
}

use ./data/dataind2, clear
local vars1="share1_t0 prod1_t0 delta_prod1"
local i=1
foreach r of local list1 {
local j=2
foreach y in "delta_shareemp1" {
reg `y' `r' `vars1' if t==1, robust
 matrix A[`i',1+(`j'-1)*2]=_b[`r']
 matrix B[`i',1+(`j'-1)*2]=_se[`r']
 matrix N[`i',1+(`j'-1)*2]=e(N)
reg `y' `r' `vars1' if t==2, robust
 matrix A[`i',1+1+(`j'-1)*2]=_b[`r']
 matrix B[`i',1+1+(`j'-1)*2]=_se[`r']
 matrix N[`i',1+1+(`j'-1)*2]=e(N)
local j=`j'+1
} 
local i=`i'+1
} 
matrix list A
matrix list B
matrix list N

* table
qui { 
set linesize 255
capture log close
log using "./results/`name'.tex", text replace
no di "\begin{tabular}"
no di "{lccccc}\hline\hline"
no di " & \multicolumn{2}{c}{\rule{0cm}{0.5cm}Share in employment} && \multicolumn{2}{c}{\rule{0cm}{0.5cm}Share in employment}  \\"
no di "& $ 2005 $ & $ 2013 $ && $ (y_{t2}-y_{t1}) $ & $ (y_{t3}-y_{t2}) $ \\"
no di "\cline{2-3} \cline{5-6}"
no di "& \rule{0cm}{0.5cm}(1) & (2) && (3) & (4) \\\hline"

local l=1
foreach h of local list1 {
  * country name
  no di "\multicolumn{4}{l}{\textbf{\rule{0cm}{0.5cm}`h'}}\\"
  * coeficientes
  no di "", _c
  forvalues j=1(1)4 {
	no di "&" %9.3f A[`l',`j'], _c
	if `j'==2 {
	no di "&", _c
	}
  }
  no di "\\"
  * se
  forvalues j=1(1)4{
	local t=abs(A[`l',`j']/B[`l',`j'])
	local stars=""
	if `t'>=abs(invnormal(0.05)) {
	  local stars="*" 
	}
	if `t'>=abs(invnormal(0.025)) {
	  local stars="**"
	}
	if `t'>=abs(invnormal(0.005)) {
	  local stars="***"
	}
	no di "&" "(" %9.2f B[`l',`j'] "`stars')", _c
	if `j'==2 {
	no di "&", _c
	}
  }
  no di "\\ \hline"
  local l=`l'+1
} 
* N
no di "\rule{0cm}{0.5cm}Obs. ", _c
forvalues j=1(1)4 {
	no di "&" %9.0f N[1,`j'], _c
	if `j'==2 {
	no di "&", _c
	}
}
  no di "\\ \hline"
no di "\end{tabular}"
log close
}

* --------------------------------------------------------
* Table 7. Employment and wages

local name="table7"

use ./data/datallm1, clear

matrix define A=J(20,6,.)
matrix define B=J(20,6,.)
matrix define N=J(20,6,.)

local i=1
local list1 ="RTC1 RTC2 RTC3 RTC4 RTC5 RTC6"
foreach r of local list1 {
local j=1
foreach y in "delta_employment" "delta_unemployment" "delta_lwage"  {
 if "`y'"=="delta_employment" | "`y'"=="delta_unemployment" {
    local vars1="lwage_t0 employment_t0"
 }
 if "`y'"=="delta_lwage" {
    local vars1="lwage_t0 employment_t0 fgt0_t0"
 }
reg `y' `r' `vars1' if t==1, robust
 matrix A[`i',1+(`j'-1)*2]=_b[`r']
 matrix B[`i',1+(`j'-1)*2]=_se[`r']
 matrix N[`i',1+(`j'-1)*2]=e(N)
reg `y' `r' `vars1' if t==2, robust
 matrix A[`i',1+1+(`j'-1)*2]=_b[`r']
 matrix B[`i',1+1+(`j'-1)*2]=_se[`r']
 matrix N[`i',1+1+(`j'-1)*2]=e(N)
local j=`j'+1
}
local i=`i'+1
}

qui { 
set linesize 255
capture log close
log using "./results/`name'.tex", text replace
no di "\begin{tabular}"
no di "{lcccccccc}\hline\hline"
no di " & \multicolumn{2}{c}{\rule{0cm}{0.5cm} Employment rate} && \multicolumn{2}{c}{\rule{0cm}{0.5cm} Unemployment Rate} && \multicolumn{2}{c}{\rule{0cm}{0.5cm} Log Wage} \\"
no di "& $ (y_{t2}-y_{t1}) $ & $ (y_{t3}-y_{t2}) $ && $ (y_{t2}-y_{t1}) $ & $ (y_{t3}-y_{t2}) $ && $ (y_{t2}-y_{t1}) $ & $ (y_{t3}-y_{t2}) $ \\"
no di "\cline{2-3} \cline{5-6} \cline{8-9}"
no di "& \rule{0cm}{0.5cm}(1) & (2) && (3) & (4) && (5) & (6) \\ \hline"

local l=1
foreach h of local list1 {
  * country name
  no di "\multicolumn{3}{l}{\textbf{\rule{0cm}{0.5cm}`h'}}\\"
  * coefs
  no di "", _c
  forvalues j=1(1)6 {
	no di "&" %9.2f A[`l',`j'], _c
	if `j'==2 | `j'==4  {
	no di "&", _c
	}
  }
  no di "\\"
  * se
  forvalues j=1(1)6{
	local t=abs(A[`l',`j']/B[`l',`j'])
	local stars=""
	if `t'>=abs(invnormal(0.05)) {
	  local stars="*" 
	}
	if `t'>=abs(invnormal(0.025)) {
	  local stars="**"
	}
	if `t'>=abs(invnormal(0.005)) {
	  local stars="***"
	}
	no di "&" "(" %9.2f B[`l',`j'] "`stars')", _c
	if `j'==2 | `j'==4  {
	no di "&", _c
	}
  }
  no di "\\ \hline"

  local l=`l'+1
}
    * N
  no di "\rule{0cm}{0.5cm}Obs. ", _c
  forvalues j=1(1)6 {
	no di "&" %9.0f N[1,`j'], _c
	if `j'==2 | `j'==4  {
	no di "&", _c
	}
  }
  no di "\\ \hline"

no di "\end{tabular}"
log close
}

* --------------------------------------------------------
* Table 8. Parameterization of technology adoption

local name="table8"

use ./data/datallm3, clear

local list1 "RTC1 RTC2 RTC3 RTC4 RTC5 RTC6"

matrix define A=J(20,5,.)
matrix define B=J(20,5,.)
matrix define N=J(20,5,.)

local v="Ddelta_robot_out"
local vars1="lwage_t0 employment_t0"
local i=1
foreach r of local list1 {
 qui gen inter=`r'*`v'
 local j=1
 foreach y in "Ddelta_employment" "Ddelta_unemployment" "Ddelta_lwage" {
 if "`y'"=="Ddelta_employment" {
    local vars1="lwage_t0 employment_t0"
 }
 if "`y'"=="Ddelta_unemployment" {
    local vars1="unemployment_t0"
 }
 if "`y'"=="Ddelta_lwage" {
    local vars1="lwage_t0 fgt0_t0 informality_1"
 }

  xi: reg `y' inter `r' `vars1' i.year if year>=2005, cluster(district)
  matrix A[`i',`j']=_b[inter]
  matrix B[`i',`j']=_se[inter]
  matrix N[`i',`j']=e(N)
  local j=`j'+1
 }
 qui drop inter
 local i=`i'+1
}

matrix list A
matrix list B
matrix list N

* table
qui { 
set linesize 255
capture log close
log using "./results/`name'.tex", text replace
no di "\begin{tabular}"
no di "{lccc}\hline\hline"
no di " & \rule{0cm}{0.5cm}Employment & Unemployment & Log Wage \\"
no di " & rate & rate &   \\"
no di "& $ (y_{t}-y_{0}) $ & $ (y_{t}-y_{0}) $ & $ (y_{t}-y_{0}) $ \\"
no di " \cline{2-4}"
no di "& \rule{0cm}{0.5cm}(1) & (2) & (3) \\ \hline"

local l=1
foreach h of local list1 {
  no di "\multicolumn{4}{l}{\textbf{\rule{0cm}{0.5cm}`h'}}\\"
  no di "", _c
  no di "$ RTC \times Tech$", _c
  forvalues j=1(1)3 {
  	no di "&" %9.3f A[`l',`j'], _c
  }
  no di "\\"
  forvalues j=1(1)3{
	local t=abs(A[`l',`j']/B[`l',`j'])
	local stars=""
	if `t'>=abs(invnormal(0.05)) {
	  local stars="*" 
	}
	if `t'>=abs(invnormal(0.025)) {
	  local stars="**"
	}
	if `t'>=abs(invnormal(0.005)) {
	  local stars="***"
	}
	no di "&" "(" %9.3f B[`l',`j'] "`stars')", _c
  }
  no di "\\ \hline"
local l=`l'+1
}
no di "\rule{0cm}{0.5cm}Obs. ", _c
forvalues j=1(1)3 {
  no di "&" %9.0f N[1,`j'], _c
}
no di "\\ \hline"
no di "\end{tabular}"
log close
}

* --------------------------------------------------------
* Table 9. Unemployment by skill groups

local name="table9"

use ./data/datallm1, clear

matrix define A=J(20,6,.)
matrix define B=J(20,6,.)
matrix define N=J(20,6,.)
 
local list1 "RTC1 RTC2 RTC3 RTC4 RTC5 RTC6"
local vars1="lwage_t0 employment_t0"
local i=1
foreach r of local list1 {
   
local j=1
foreach y in "delta_unemployment_sk1" "delta_unemployment_sk2" "delta_unemployment_sk3"  {
reg `y' `r' `vars1' if t==1, robust
 matrix A[`i',1+(`j'-1)*2]=_b[`r']
 matrix B[`i',1+(`j'-1)*2]=_se[`r']
 matrix N[`i',1+(`j'-1)*2]=e(N)
reg `y' `r' `vars1' if t==2, robust
 matrix A[`i',1+1+(`j'-1)*2]=_b[`r']
 matrix B[`i',1+1+(`j'-1)*2]=_se[`r']
 matrix N[`i',1+1+(`j'-1)*2]=e(N)
local j=`j'+1
}
local i=`i'+1
}

* table
qui { 
set linesize 255
capture log close
log using "./results/`name'.tex", text replace
no di "\begin{tabular}"
no di "{lcccccccc}\hline\hline"
no di " & \multicolumn{2}{c}{\rule{0cm}{0.5cm} Unskilled} && \multicolumn{2}{c}{\rule{0cm}{0.5cm} Semi-skilled} && \multicolumn{2}{c}{\rule{0cm}{0.5cm} Highly skilled}  \\"
no di "& $ (y_{t2}-y_{t1}) $ & $ (y_{t3}-y_{t2}) $ && $ (y_{t2}-y_{t1}) $ & $ (y_{t3}-y_{t2}) $ && $ (y_{t2}-y_{t1}) $ & $ (y_{t3}-y_{t2}) $ \\"
no di "\cline{2-3} \cline{5-6} \cline{8-9}"
no di "& \rule{0cm}{0.5cm}(1) & (2) && (3) & (4) && (5) & (6)\\ \hline"

local l=1
foreach h of local list1 {
  * country name
  no di "\multicolumn{5}{l}{\textbf{\rule{0cm}{0.5cm}`h'}}\\"
  * coefs
  no di "", _c
  forvalues j=1(1)6 {
	no di "&" %9.2f A[`l',`j'], _c
	if `j'==2 | `j'==4  {
	no di "&", _c
	}
  }
  no di "\\"
  * se
  forvalues j=1(1)6{
	local t=abs(A[`l',`j']/B[`l',`j'])
	local stars=""
	if `t'>=abs(invnormal(0.05)) {
	  local stars="*" 
	}
	if `t'>=abs(invnormal(0.025)) {
	  local stars="**"
	}
	if `t'>=abs(invnormal(0.005)) {
	  local stars="***"
	}
	no di "&" "(" %9.2f B[`l',`j'] "`stars')", _c
	if `j'==2 | `j'==4  {
	no di "&", _c
	}
  }
  no di "\\ \hline"
  local l=`l'+1
}
    * N
  no di "\rule{0cm}{0.5cm}Obs. ", _c
  forvalues j=1(1)6 {
	no di "&" %9.0f N[1,`j'], _c
	if `j'==2 | `j'==4  {
	no di "&", _c
	}
  }
  no di "\\ \hline"
no di "\end{tabular}"
log close
}

* --------------------------------------------------------
* Table 10. Informality by skill groups

local name="table10"

use ./data/datallm1, clear

matrix define A=J(20,6,.)
matrix define B=J(20,6,.)
matrix define N=J(20,6,.)
 
local list1 "RTC1 RTC2 RTC3 RTC4 RTC5 RTC6"
local vars1="lwage_t0 employment_t0"
local i=1
foreach r of local list1 {
local j=1
foreach y in "delta_informality_1_sk1" "delta_informality_1_sk2" "delta_informality_1_sk3"  {
reg `y' `r' `vars1' if t==1, robust
 matrix A[`i',1+(`j'-1)*2]=_b[`r']
 matrix B[`i',1+(`j'-1)*2]=_se[`r']
 matrix N[`i',1+(`j'-1)*2]=e(N)
reg `y' `r' `vars1' if t==2, robust
 matrix A[`i',1+1+(`j'-1)*2]=_b[`r']
 matrix B[`i',1+1+(`j'-1)*2]=_se[`r']
 matrix N[`i',1+1+(`j'-1)*2]=e(N)
local j=`j'+1
} /* cierra foreach y */
local i=`i'+1
} /* cierra foreach r */

* table
qui { 
set linesize 255
capture log close
log using "./results/`name'.tex", text replace
no di "\begin{tabular}"
no di "{lcccccccc}\hline\hline"
no di " & \multicolumn{2}{c}{\rule{0cm}{0.5cm} Unskilled} && \multicolumn{2}{c}{\rule{0cm}{0.5cm} Semi-skilled} && \multicolumn{2}{c}{\rule{0cm}{0.5cm} Highly skilled}  \\"
no di "& $ (y_{t2}-y_{t1}) $ & $ (y_{t3}-y_{t2}) $ && $ (y_{t2}-y_{t1}) $ & $ (y_{t3}-y_{t2}) $ && $ (y_{t2}-y_{t1}) $ & $ (y_{t3}-y_{t2}) $ \\"
no di "\cline{2-3} \cline{5-6} \cline{8-9}"
no di "& \rule{0cm}{0.5cm}(1) & (2) && (3) & (4) && (5) & (6)\\ \hline"
local l=1
foreach h of local list1 {
  * country name
  no di "\multicolumn{5}{l}{\textbf{\rule{0cm}{0.5cm}`h'}}\\"
  * coefs
  no di "", _c
  forvalues j=1(1)6 {
	no di "&" %9.2f A[`l',`j'], _c
	if `j'==2 | `j'==4  {
	no di "&", _c
	}
  }
  no di "\\"
  * se
  forvalues j=1(1)6{
	local t=abs(A[`l',`j']/B[`l',`j'])
	local stars=""
	if `t'>=abs(invnormal(0.05)) {
	  local stars="*" 
	}
	if `t'>=abs(invnormal(0.025)) {
	  local stars="**"
	}
	if `t'>=abs(invnormal(0.005)) {
	  local stars="***"
	}
	no di "&" "(" %9.2f B[`l',`j'] "`stars')", _c
	if `j'==2 | `j'==4  {
	no di "&", _c
	}
  }
  no di "\\ \hline"
  local l=`l'+1
}
    * N
  no di "\rule{0cm}{0.5cm}Obs. ", _c
  forvalues j=1(1)6 {
	no di "&" %9.0f N[1,`j'], _c
	if `j'==2 | `j'==4  {
	no di "&", _c
	}
  }
  no di "\\ \hline"
no di "\end{tabular}"
log close
}

* --------------------------------------------------------
* Table 11. Poverty and Inequality

local name="table11"

use ./data/datallm1, clear

matrix define A=J(20,6,.)
matrix define B=J(20,6,.)
matrix define N=J(20,6,.)

local list1 "RTC1 RTC2 RTC3 RTC4 RTC5 RTC6"
local i=1
foreach r of local list1 {
local j=1
local y="delta_fgt0_of"
local vars1="fgt0_t0 ratio7525_ipcf_t0"
reg `y' `r' `vars1' if t==1, robust
 matrix A[`i',1+(`j'-1)*2]=_b[`r']
 matrix B[`i',1+(`j'-1)*2]=_se[`r']
 matrix N[`i',1+(`j'-1)*2]=e(N)
reg `y' `r' `vars1' if t==2, robust
 matrix A[`i',1+1+(`j'-1)*2]=_b[`r']
 matrix B[`i',1+1+(`j'-1)*2]=_se[`r']
 matrix N[`i',1+1+(`j'-1)*2]=e(N)
local j=`j'+1

local y="delta_ratio7525_ipcf"
local vars1="fgt0_t0 fgt1_t0 ratio7525_ipcf_t0"
reg `y' `r' `vars1' if t==1, robust
 matrix A[`i',1+(`j'-1)*2]=_b[`r']
 matrix B[`i',1+(`j'-1)*2]=_se[`r']
 matrix N[`i',1+(`j'-1)*2]=e(N)
reg `y' `r' `vars1' if t==2, robust
 matrix A[`i',1+1+(`j'-1)*2]=_b[`r']
 matrix B[`i',1+1+(`j'-1)*2]=_se[`r']
 matrix N[`i',1+1+(`j'-1)*2]=e(N)
local j=`j'+1

local i=`i'+1
} 

* table
qui { 
set linesize 255
capture log close
log using "./results/`name'.tex", text replace
no di "\begin{tabular}"
no di "{lcccccc}\hline\hline"
no di " & \multicolumn{2}{c}{\rule{0cm}{0.5cm} Poverty rate} && \multicolumn{2}{c}{\rule{0cm}{0.5cm} Inequality p75--p25} \\"
no di "& $ (y_{t2}-y_{t1}) $ & $ (y_{t3}-y_{t2}) $ && $ (y_{t2}-y_{t1}) $ & $ (y_{t3}-y_{t2}) $ \\"
no di "\cline{2-3} \cline{5-6}"
no di "& \rule{0cm}{0.5cm}(1) & (2) && (3) & (4) \\ \hline"
local l=1
foreach h of local list1 {
  * country name
  no di "\multicolumn{3}{l}{\textbf{\rule{0cm}{0.5cm}`h'}}\\"
  * coefs
  no di "", _c
  forvalues j=1(1)4 {
	no di "&" %9.2f A[`l',`j'], _c
	if `j'==2 | `j'==4  {
	no di "&", _c
	}
  }
  no di "\\"
  * se
  forvalues j=1(1)4{
	local t=abs(A[`l',`j']/B[`l',`j'])
	local stars=""
	if `t'>=abs(invnormal(0.05)) {
	  local stars="*" 
	}
	if `t'>=abs(invnormal(0.025)) {
	  local stars="**"
	}
	if `t'>=abs(invnormal(0.005)) {
	  local stars="***"
	}
	no di "&" "(" %9.2f B[`l',`j'] "`stars')", _c
	if `j'==2 | `j'==4  {
	no di "&", _c
	}
  }
  no di "\\ \hline"

  local l=`l'+1
}
    * N
  no di "\rule{0cm}{0.5cm}Obs. ", _c
  forvalues j=1(1)4 {
	no di "&" %9.0f N[1,`j'], _c
	if `j'==2 | `j'==4  {
	no di "&", _c
	}
  }
  no di "\\ \hline"
no di "\end{tabular}"
log close
}

* --------------------------------------------------------
* Table A1. PIAAC surveys

local name="tableA1"

use ./data/piaac1, clear
gen group=isic4_c
drop if substr(string(group),1,2)=="99"
drop if length(string(group))<4

qui { 
set linesize 255
capture log close
log using "./results/`name'.tex", text replace
no di "\begin{tabular}"
no di "{lccccc}\hline\hline"
no di "& \rule{0cm}{0.5cm} All & Chile & Ecuador & Mexico & Peru \\"
no di "& Surveys \\"
no di "& (1) & (2) & (3) & (4) & (5) \\ \hline"

gen var="\rule{0cm}{0.5cm}Supervising" in 1
replace var="Planning" in 2
replace var="Solving problems" in 3
replace var="Written output" in 4
replace var="Presentations" in 5
replace var="Budgets" in 6
replace var="\rule{0cm}{0.5cm}F1" in 7
replace var="F2" in 8
replace var ="F3" in 9
replace var="F4" in 10
replace var="F5" in 11
replace var="\rule{0cm}{0.5cm}Observations" in 12

local i=1
foreach x of varlist supervise1 planning1 problems1 write1 sales1 budget1 F1 F2 F3 F4{
	if `i'==7 {
		no di "\hline"
	}
	local name=var[`i']
	sum `x' [weight= spfwt0] if country=="Chile" | country=="Ecuador" | country=="Mexico" | country=="Peru"
	local b=r(mean)
	sum `x' [weight= spfwt0] if country=="Chile"
	local c=r(mean)
	sum `x' [weight= spfwt0] if country=="Ecuador"
	local d=r(mean)
	sum `x' [weight= spfwt0] if country=="Mexico"
	local e=r(mean)
	sum `x' [weight= spfwt0] if country=="Peru"
	local f=r(mean)
	no di " `name' & " %9.2f `b' "&" %9.2f `c' "&" %9.2f `d' "&" %9.2f `e' "&" %9.2f `f' "\\"
	local i=`i'+1
}

foreach x of varlist F6 {
	if `i'==7 {
		no di "\hline"
	}
	local name=var[`i']
	sum `x' if country=="Chile" | country=="Ecuador" | country=="Mexico" | country=="Peru"
	local b=r(mean)
	sum `x' if country=="Chile"
	local c=r(mean)
	sum `x' if country=="Ecuador"
	local d=r(mean)
	sum `x' if country=="Mexico"
	local e=r(mean)
	sum `x' if country=="Peru"
	local f=r(mean)
	no di " `name' & " %9.2f `b' "&" %9.2f `c' "&" %9.2f `d' "&" %9.2f `e' "&" %9.2f `f' "\\"
	local i=`i'+1
}

local x="F1"
local name=var[`i']
sum `x' if country=="Chile" | country=="Ecuador" | country=="Mexico" | country=="Peru"
local b=r(N)
sum `x' if country=="Chile"
local c=r(N)
sum `x' if country=="Ecuador"
local d=r(N)
sum `x' if country=="Mexico"
local e=r(N)
sum `x' if country=="Peru"
local f=r(N)
no di "\hline"
no di " `name' & " %9.0f `b' "&" %9.0f `c' "&" %9.0f `d' "&" %9.0f `e' "&" %9.0f `f' "\\\hline"
no di "\end{tabular}"
log close
}

* --------------------------------------------------------
* Table A2. Correlation of PIAAC RTC indexes computed from different samples

local name="tableA2"
use ./data/piaac1, clear

* occupation
gen group=isco08_c
drop if substr(string(group),1,2)=="99"
drop if length(string(group))<4

* Pooled
preserve
keep if country=="Mexico" | country=="Chile" | country=="Ecuador" | country=="Peru"
bys group: egen aux0=sum(spfwt0)
forvalues i=1(1)5 {
	bys group: egen RTC`i'_lac=sum(spfwt0*F`i')
	replace RTC`i'_lac=RTC`i'_lac/aux0
}
keep group RTC*
bys group: gen n_lac=_N
bys group: drop if _n>1
sort group
tempfile temp1_lac
save `temp1_lac', replace
restore

* By country
foreach x in "Mexico" "Chile" "Ecuador" "Peru" {
	preserve
	keep if country=="`x'"
	bys group: egen aux0=sum(spfwt0)
	forvalues i=1(1)5 {
		bys group: egen RTC`i'_`x'=sum(spfwt0*F`i')
		replace RTC`i'_`x'=RTC`i'_`x'/aux0
	}
	keep group RTC*
	bys group: gen n_`x'=_N
	bys group: drop if _n>1
	sort group
	tempfile temp1_`x'
	save `temp1_`x'', replace
	restore
}

* Merge
use `temp1_lac', clear
*erase temp1_lac.dta
foreach x in "Mexico" "Chile" "Ecuador" "Peru" {
	merge 1:1 group using `temp1_`x'', nogen
*	erase `temp1_`x''.dta
}

* keep occupations with sufficiently large number of obs
foreach x of varlist n_lac n_Chile n_Peru n_Ecuador n_Mexico { 
	drop if `x'<25
	drop if `x'==.
	}
count
local n=r(N)

forvalues i=1(1)5 {
	correlate RTC`i'_lac RTC`i'_Chile RTC`i'_Ecuador RTC`i'_Mexico RTC`i'_Peru
	matrix define A=r(C)
	matrix define B`i'=A[2..5,1]
	matrix list B`i'
}

qui { 
set linesize 255
capture log close
log using "./results/`name'.tex", text replace
no di "\begin{tabular}"
no di "{lccccc}\hline\hline"
no di " & \multicolumn{5}{c}{\rule{0cm}{0.5cm}All Latin American surveys}\\"
no di "\cline{2-6}"
no di " & \rule{0cm}{0.5cm}RTC1 & RTC2 & RTC3 & RTC4 & RTC5\\"
no di " & (1) & (2) & (3) & (4) & (5)\\"
no di " \hline"
no di " \multicolumn{6}{l}{\rule{0cm}{0.5cm}\textbf{Panel A: Occupation-level index}}\\"
no di "Chile & " %9.2f B1[1,1] "&" %9.2f B2[1,1] "&" %9.2f B3[1,1] "&" %9.2f B4[1,1] "&" %9.2f B5[1,1] "\\"
no di "Ecuador & " %9.2f B1[2,1] "&" %9.2f B2[2,1] "&" %9.2f B3[2,1] "&" %9.2f B4[2,1] "&" %9.2f B5[2,1] "\\"
no di "Mexico & " %9.2f B1[3,1] "&" %9.2f B2[3,1] "&" %9.2f B3[3,1] "&" %9.2f B4[3,1] "&" %9.2f B5[3,1] "\\"
no di "Peru & " %9.2f B1[4,1] "&" %9.2f B2[4,1] "&" %9.2f B3[4,1] "&" %9.2f B4[4,1] "&" %9.2f B5[4,1] "\\"
no di "\hline"
log close
}

* industry
use ./data/piaac1, clear

gen group=isic4_c
drop if substr(string(group),1,2)=="99"
drop if length(string(group))<4

* pooled
preserve
keep if country=="Mexico" | country=="Chile" | country=="Ecuador" | country=="Peru"
bys group: egen aux0=sum(spfwt0)
forvalues i=1(1)5 {
	bys group: egen RTC`i'_lac=sum(spfwt0*F`i')
	replace RTC`i'_lac=RTC`i'_lac/aux0
}
keep group RTC*
bys group: gen n_lac=_N
bys group: drop if _n>1
sort group
tempfile temp1_lac
save `temp1_lac', replace
restore

* By country
foreach x in "Mexico" "Chile" "Ecuador" "Peru" {
	preserve
	keep if country=="`x'"
	bys group: egen aux0=sum(spfwt0)
	forvalues i=1(1)5 {
		bys group: egen RTC`i'_`x'=sum(spfwt0*F`i')
		replace RTC`i'_`x'=RTC`i'_`x'/aux0
	}
	keep group RTC*
	bys group: gen n_`x'=_N
	bys group: drop if _n>1
	sort group
	tempfile temp1_`x'
	save `temp1_`x'', replace
	restore
}

* Merge
use `temp1_lac', clear
*erase temp1_lac.dta
foreach x in "Mexico" "Chile" "Ecuador" "Peru" {
	merge 1:1 group using `temp1_`x'', nogen
*	erase temp1_`x'.dta
}

* keep industries with sufficiently large number of obs
foreach x of varlist n_lac n_Chile n_Peru n_Ecuador n_Mexico { 
	drop if `x'<25
	drop if `x'==.
	}

count
local n=r(N)

forvalues i=1(1)5 {
	correlate RTC`i'_lac RTC`i'_Chile RTC`i'_Ecuador RTC`i'_Mexico RTC`i'_Peru
	matrix define A=r(C)
	matrix define B`i'=A[2..5,1]
	matrix list B`i'
}

qui { 
set linesize 255
capture log close
log using "./results/`name'.tex", text append
no di " \multicolumn{6}{l}{\rule{0cm}{0.5cm}\textbf{Panel B: Industry-level index}}\\"
no di "Chile & " %9.2f B1[1,1] "&" %9.2f B2[1,1] "&" %9.2f B3[1,1] "&" %9.2f B4[1,1]  "&" %9.2f B5[1,1] "\\"
no di "Ecuador & " %9.2f B1[2,1] "&" %9.2f B2[2,1] "&" %9.2f B3[2,1] "&" %9.2f B4[2,1] "&" %9.2f B5[2,1] "\\"
no di "Mexico & " %9.2f B1[3,1] "&" %9.2f B2[3,1] "&" %9.2f B3[3,1] "&" %9.2f B4[3,1]  "&" %9.2f B5[3,1] "\\"
no di "Peru & " %9.2f B1[4,1] "&" %9.2f B2[4,1] "&" %9.2f B3[4,1] "&" %9.2f B4[4,1]  "&" %9.2f B5[4,1] "\\"
no di "\hline"
no di "\end{tabular}"
log close
}

* --------------------------------------------------------
* * Table A3. List of industries
local name="tableA3"
use ./data/piaac3, clear

bys isic: egen aux=mean(RTJ8)
bys isic: drop if _n>1
drop if nlac==1

qui { 
set linesize 255
capture log close
log using "./results/`name'.tex", text replace
no di "\begin{tabular}"
no di "{lccccccc}\hline\hline"
no di " \rule{0cm}{0.5cm}Industry & RTC1 & RTC2 & RTC3 & RTC4 & RTC5 & RTC6 & Obs. \\"
no di "& (1) & (2) & (3) & (4) & (5) & (6) & (7) \\ \hline"
count
local n=r(N)
sort isic
no di "\rule{0cm}{0.5cm}"isicname[1] "&" %9.2f RTC1lac[1] "&" %9.2f RTC2lac[1] "&" %9.2f RTC3lac[1] "&" %9.2f RTC4lac[1] "&" %9.2f RTC5lac[1] "&" %9.2f aux[1] "&" %9.0f nlac[1] "\\"
forvalues i=2(1)`n' {
  no di isicname[`i'] "&" %9.2f RTC1lac[`i'] "&" %9.2f RTC2lac[`i'] "&" %9.2f RTC3lac[`i'] "&" %9.2f RTC4lac[`i'] "&" %9.2f RTC5lac[`i'] "&" %9.2f aux[`i'] "&" %9.0f nlac[`i'] "\\"
}
no di "\hline"
no di "\end{tabular}"
log close
}

* Figure A1, panel A. Correlation in industry RTC indexes
local name="figureA1"
sort isic
forvalues i=2(1)5 {
	correlate RTC1lac RTC`i'lac
	local r = round(1000*r(rho))/1000
	di `r'
	twoway (scatter RTC`i'lac RTC1lac) (lfit RTC`i'lac RTC1lac, lcolor(gs8) lwidth(thick)), ytitle(RTC`i') xtitle(RTC1) legend(off) /*note(rho=`r')*/
	graph export "./results/`name'-h`i'.png", replace width(1500)
}
local i=6
correlate RTC1lac aux
local r = round(1000*r(rho))/1000
di `r'
twoway (scatter aux RTC1lac) (lfit aux RTC1lac, lcolor(gs8) lwidth(thick)), ytitle(RTC`i') xtitle(RTC1) legend(off) /*note(rho=`r')*/
graph export "./results/`name'-h`i'.png", replace width(1500)

* --------------------------------------------------------
* Table A4. List of occupations

use ./data/piaac2, clear
local name="tableA4"

qui { 
set linesize 255
capture log close
log using "./results/`name'.tex", text replace
no di "\begin{tabular}"
no di "{lccccccc}\hline\hline"
no di " \rule{0cm}{0.5cm}Occupation & RTC1 & RTC2 & RTC3 & RTC4 & RTC5 & RTC6 & Obs. \\"
no di "& (1) & (2) & (3) & (4) & (5) & (6) & (7)\\ \hline"
count
local n=r(N)
sort occ
no di "\rule{0cm}{0.5cm}"occname[1] "&" %9.2f RTC1lac[1] "&" %9.2f RTC2lac[1] "&" %9.2f RTC3lac[1] "&" %9.2f RTC4lac[1] "&" %9.2f RTC5lac[1] "&" %9.2f RTC8[1] "&" %9.0f nlac[1] "\\"
forvalues i=2(1)`n' {
  no di occname[`i'] "&" %9.2f RTC1lac[`i'] "&" %9.2f RTC2lac[`i'] "&" %9.2f RTC3lac[`i'] "&" %9.2f RTC4lac[`i'] "&" %9.2f RTC5lac[`i'] "&" %9.2f RTC8[`i'] "&" %9.0f nlac[`i'] "\\"
}
no di "\hline"
no di "\end{tabular}"
log close
}

* Figure A1, panel b. Correlation in occupation RTC indexes
local name="figureA1"
sort occ
forvalues i=2(1)5 {
	correlate RTC1lac RTC`i'lac
	local r = round(1000*r(rho))/1000
	di `r'
	twoway (scatter RTC`i'lac RTC1lac) (lfit RTC`i'lac RTC1lac, lcolor(gs8) lwidth(thick)), ytitle(RTC`i') xtitle(RTC1) legend(off) /*note(rho=`r')*/
	graph export "./results/`name'-g`i'.png", replace width(1500)
}
local i=6
correlate RTC1lac RTC8
local r = round(1000*r(rho))/1000
di `r'
twoway (scatter RTC8 RTC1lac) (lfit RTC8 RTC1lac, lcolor(gs8) lwidth(thick)), ytitle(RTC`i') xtitle(RTC1) legend(off) /*note(rho=`r')*/
graph export "./results/`name'-g`i'.png", replace width(1500)

* --------------------------------------------------------
* Table A5. Average RTC by country

use ./data/datallm1, clear
local name="tableA5"

bys country district: keep if _n==1
keep country district sh_pop RTC1-RTC6
forvalues i=1(1)6 {
  bys country: egen aux`i'=sum(RTC`i'*sh_pop)
  drop RTC`i'
  rename aux`i' RTC`i'
}
bys country: keep if _n==1
drop district sh_pop
sort country
gen cname="Argentina"
replace cname="Brazil" if country=="BRA"
replace cname="Chile" if country=="CHL"
replace cname="Colombia" if country=="COL"
replace cname="Ecuador" if country=="ECU"
replace cname="Mexico" if country=="MEX"
replace cname="Peru" if country=="PER"

qui { 
set linesize 255
capture log close
log using "./results/`name'.tex", text replace
no di "\begin{tabular}"
no di "{lcccccc}\hline\hline"
no di " & \rule{0cm}{0.5cm}RTC1 & RTC2 & RTC3 & RTC4 & RTC5 & RTC6 \\"
no di "& (1) & (2) & (3) & (4) & (5) & (6) \\ \hline"
no di " \rule{0cm}{0.5cm}\textbf{Countries} &  &  & &  &  & \\"
local i=1
no di "\rule{0cm}{0.5cm}" cname[`i'] "&" %9.2f RTC1[`i'] "&" %9.2f RTC2[`i'] "&" %9.2f RTC3[`i'] "&" %9.2f RTC4[`i'] "&" %9.2f RTC5[`i'] "&" %9.2f RTC6[`i']  "\\"
forvalues i=2(1)7 {
  no di cname[`i'] "&" %9.2f RTC1[`i'] "&" %9.2f RTC2[`i'] "&" %9.2f RTC3[`i'] "&" %9.2f RTC4[`i'] "&" %9.2f RTC5[`i'] "&" %9.2f RTC6[`i']  "\\"
}
no di "\hline"
no di "\end{tabular}"
log close
}


* --------------------------------------------------------
* Table A6. Industry level

local name="tableA6"

use ./data/dataind2, clear

matrix define A=J(20,6,.)
matrix define B=J(20,6,.)
matrix define N=J(20,6,.)

local list1 "RTC1 RTC2 RTC3 RTC4 RTC5 RTC6"
matrix define A=J(20,6,.)
matrix define B=J(20,6,.)
matrix define N=J(20,6,.)
local i=1
foreach r of local list1 {
local j=1
foreach y in "delta_lemp" "delta_lwage" "delta_share1"    {
local vars1="OFF share1_t0 prod1_t0 delta_prod1"
reg `y' `r' `vars1' if t==1, robust
 matrix A[`i',1+(`j'-1)*2]=_b[`r']
 matrix B[`i',1+(`j'-1)*2]=_se[`r']
 matrix N[`i',1+(`j'-1)*2]=e(N)
reg `y' `r' `vars1' if t==2, robust
 matrix A[`i',1+1+(`j'-1)*2]=_b[`r']
 matrix B[`i',1+1+(`j'-1)*2]=_se[`r']
 matrix N[`i',1+1+(`j'-1)*2]=e(N)
local j=`j'+1
} 
local i=`i'+1
} 

* table
qui { 
set linesize 255
capture log close
log using "./results/`name'.tex", text replace
no di "\begin{tabular}"
no di "{lcccccccc}\hline\hline"
no di " & \multicolumn{2}{c}{\rule{0cm}{0.5cm}Log employment} && \multicolumn{2}{c}{\rule{0cm}{0.5cm}Log wage} && \multicolumn{2}{c}{\rule{0cm}{0.5cm}Labor share}  \\"
no di "& $ (y_{t2}-y_{t1}) $ & $ (y_{t3}-y_{t2}) $ && $ (y_{t2}-y_{t1}) $ & $ (y_{t3}-y_{t2}) $ && $ (y_{t2}-y_{t1}) $ & $ (y_{t3}-y_{t2}) $ \\"
no di "\cline{2-3} \cline{5-6} \cline{8-9}"
no di "& \rule{0cm}{0.5cm}(1) & (2) && (3) & (4) && (5) & (6)\\ \hline"
local l=1
foreach h of local list1 {
  * country name
  no di "\multicolumn{5}{l}{\textbf{\rule{0cm}{0.5cm}`h'}}\\"
  * coefs
  no di "", _c
  forvalues j=1(1)6 {
	no di "&" %9.2f A[`l',`j'], _c
	if `j'==2 | `j'==4  {
	no di "&", _c
	}
  }
  no di "\\"
  * se
  forvalues j=1(1)6{
	local t=abs(A[`l',`j']/B[`l',`j'])
	local stars=""
	if `t'>=abs(invnormal(0.05)) {
	  local stars="*" 
	}
	if `t'>=abs(invnormal(0.025)) {
	  local stars="**"
	}
	if `t'>=abs(invnormal(0.005)) {
	  local stars="***"
	}
	no di "&" "(" %9.2f B[`l',`j'] "`stars')", _c
	if `j'==2 | `j'==4  {
	no di "&", _c
	}
  }
  no di "\\ \hline"
  local l=`l'+1
}
    * N
  no di "\rule{0cm}{0.5cm}Obs. ", _c
  forvalues j=1(1)6 {
	no di "&" %9.0f N[1,`j'], _c
	if `j'==2 | `j'==4  {
	no di "&", _c
	}
  }
  no di "\\ \hline"
no di "\end{tabular}"
log close
}

* --------------------------------------------------------
* Table A7. Control for offshoring

local name="tableA7"

use ./data/dataind3, clear
matrix define A=J(20,6,.)
matrix define B=J(20,6,.)
matrix define N=J(20,6,.)

local list1 "RTC1 RTC2 RTC3 RTC4 RTC5 RTC6"
local r="RTC1"
local v="Ddelta_robot_wrk"
gen offinter=OFF*`v'
local vars1="offinter share1_t0 prod1_t0 Ddelta_prod1"
local i=1
foreach r of local list1 {
 qui gen inter=`r'*`v'
 local j=1
 foreach y in "Ddelta_lemp" "Ddelta_lwage" "Ddelta_share1" "Ddelta_shareemp1" "Ddelta_shareout" "Ddelta_lout" {
  xi: reg `y' inter `r' `vars1' i.year if year>=2004, robust
  matrix A[`i',`j']=_b[inter]
  matrix B[`i',`j']=_se[inter]
  matrix N[`i',`j']=e(N)
  local j=`j'+1
 }
 qui drop inter
 local i=`i'+1
}

* table
qui { 
set linesize 255
capture log close
log using "./results/`name'.tex", text replace
no di "\begin{tabular}"
no di "{lccc}\hline\hline"
no di " & \rule{0cm}{0.5cm}Log employment & Log wage & Labor share  \\"
no di "& \rule{0cm}{0.5cm}(1) & (2) & (3) \\ \hline"
local l=1
foreach h of local list1 {
  no di "\multicolumn{4}{l}{\textbf{\rule{0cm}{0.5cm}`h'}}\\"
  no di "", _c
  no di "$ RTC \times Tech$", _c
  forvalues j=1(1)3 {
  	no di "&" %9.2f A[`l',`j'], _c
  }
  no di "\\"
  forvalues j=1(1)3{
	local t=abs(A[`l',`j']/B[`l',`j'])
	local stars=""
	if `t'>=abs(invnormal(0.05)) {
	  local stars="*" 
	}
	if `t'>=abs(invnormal(0.025)) {
	  local stars="**"
	}
	if `t'>=abs(invnormal(0.005)) {
	  local stars="***"
	}
	no di "&" "(" %9.2f B[`l',`j'] "`stars')", _c
  }
  no di "\\ \hline"
local l=`l'+1
}
no di "\rule{0cm}{0.5cm}Obs. ", _c
forvalues j=1(1)3 {
  no di "&" %9.0f N[1,`j'], _c
}
no di "\\ \hline"
no di "\end{tabular}"
log close
}

